package com.asdf.db.operations.user;

import java.util.List;

import com.asdf.db.operations.IDBOperation;
import com.asdf.db.results.ResultStatus;
import com.asdf.db.results.user.UserResult;
import com.asdf.external.DBUserDetails;

/** Operation for searching user by his username.
 * @author so4i2014
 *
 */
public class GetUserByUsernameOp implements IDBOperation<UserResult>
{
	private List<DBUserDetails> userDataSource = null;
	private String username = null;

	/** Create a new operation.
	 * @param source - list to look for in.
	 */
	public GetUserByUsernameOp(List<DBUserDetails> source)
	{
		userDataSource = source;
	}
	
	/** Set the username we are looking for.
	 * @param username
	 */
	public void setUsername(String username)
	{
		this.username = username;
	}

	/* (non-Javadoc)
	 * @see com.asdf.db.operations.IDBOperation#perform()
	 */
	@Override
	public UserResult perform()
	{
		UserResult result = new UserResult();
		DBUserDetails user = null;
		
		result.setStatus(ResultStatus.NotFound);
		for (int i = 0; i < userDataSource.size(); i++)
		{
			user = userDataSource.get(i);
			if (user.getUsername().equals(this.username))
			{
				result.setStatus(ResultStatus.OK);
				result.setUser(user);
				break;
			}
		}
		return result;
	}
	
}